#!/bin/bash
function hive_start() {
  # 启动之前先判断，有没有已经启动hiveserver2
  if nc -z localhost 10000
  then echo "HiveServer2 已经启动"
  else nohup hive --service hiveserver2 1>/dev/null 2>&1 &
  fi
}

function hive_stop() {
  PID=$(ps -ef | grep HiveServer2 | grep -v grep | awk '{print $2}')
  # 如果查到PID再关闭
  if [ $PID ]
  then kill $PID
  fi
}

case $1 in
"start")
  hive_start
;;
"stop")
  hive_stop
;;
"restart")
  hive_stop
  sleep 1
  hive_start
;;
*)
  echo "Usage hiveservice.sh start | stop | restart"
;;
esac
